/*
 * This file is part of aKoustiK.

 * aKoustiK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * aKoustiK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with aKoustiK.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtGui/QLabel>
#include <QtGui/QListWidget>
#include <QtGui/QVBoxLayout>

#include <KGlobal>
#include <KSharedConfig>
#include <KSharedConfigPtr>

#include <KIcon>
#include <KStandardDirs>
#include <KToolBar>

#include <ui/playlists/playlistswidget.h>

PlaylistsWidget::PlaylistsWidget(QWidget* parent) : QWidget(parent) {
    qDebug("PlaylistsWidget: constructor invoked");
    playlistsFolderPath = KStandardDirs::locateLocal("appdata", "playlists/", true);

    listWidget = new QListWidget(this);
    listWidget->setEditTriggers(QListWidget::SelectedClicked|QListWidget::DoubleClicked);
    refresh();

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(new QLabel("Playlists"));
    layout->addWidget(listWidget);

    KToolBar* toolbar = new KToolBar(this);
    toolbar->addAction(KIcon("view-refresh"), "Refresh", this, SLOT(refresh()));
    layout->addWidget(toolbar);
}

PlaylistsWidget::~PlaylistsWidget() {
    qDebug("PlaylistsWidget: destructor invoked");
}

void PlaylistsWidget::refresh() {
    listWidget->clear();
    QString currentPlaylist = KGlobal::config()->group("Playlist").readEntry("current", QString());
    QDir dir(playlistsFolderPath);
    QStringList lists = dir.entryList(QStringList("*.xspf"), (QDir::Files|QDir::Readable), (QDir::Name|QDir::IgnoreCase));
    if (!lists.isEmpty()) {
        for (int i = 0; i < lists.size(); i++) {
            QString& name = lists[i];
            bool needBold = currentPlaylist == name;
            name.remove(".xspf", Qt::CaseInsensitive);
            QListWidgetItem* item = new QListWidgetItem(name, listWidget);
            item->setFlags(Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
            if (needBold) {
                QFont font = item->font();
                font.setBold(true);
                item->setFont(font);
            }
        }
    }
}
